package com.SyDeck.sdPerkSystem.PerksCmds;


import com.SyDeck.sdPerkSystem.sdP;
import java.util.HashMap;
import org.bukkit.ChatColor;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;

/**
 *
 * @author Deck & Sammy
 */
public class Teleport implements CommandExecutor {

    sdP plugin;
    public static HashMap<Player, Player> BringToList = new HashMap<Player, Player>();

    public Teleport(sdP plugin) {
        this.plugin = plugin;
    }

    @Override
    public boolean onCommand(CommandSender cs, Command cmnd, String string, String[] strings) {
        String[] Nargs = strings;
        String cmd = cmnd.getName().toLowerCase();
        if (cmd.equals("bringto")) {
            return TelepotMethod(cs, Nargs);
        } else if (cmd.equals("accept")) {
            return AcceptMethod(cs, Nargs);
        }
        return true;
    }

    private boolean TelepotMethod(CommandSender cs, String[] args) {
        try {
            Player ply = (Player) cs;
            if (args.length >= 1) {
                Player Friend = plugin.getServer().getPlayer(args[0]);
                String Reason = "";
                for (int x = 1; x < args.length; x++) {
                    Reason = Reason + " " + args[x];
                }
                BringToList.put(Friend, ply);
                Friend.sendMessage(ChatColor.GREEN + "Write /accept to confirm teleport invite.");
                Friend.sendMessage(ChatColor.GREEN + ply.getName() + " is asking you to join him, because:");
                Friend.sendMessage(ChatColor.GREEN + "Reason: " + ChatColor.GOLD + Reason);
                return true;
            }
            return false;
        } catch (Exception e) {
            cs.sendMessage(ChatColor.RED + "Input error, please try again.");
            return false;
        }
    }

    private boolean AcceptMethod(CommandSender cs, String[] args) {
        Player ply = (Player) cs;
        try {
            if (args.length <= 0) {
                ply.teleport(BringToList.get(ply));
                return true;
            }
            return false;
        } catch (Exception e) {
            cs.sendMessage(ChatColor.RED + "Input error, please try again.");
            return false;
        }
    }
}
